﻿#include<iostream>
using namespace std;

int ans[10000], mid[40], front[40];

void solve(int p1, int p2, int q1, int q2, int pos)
{
	if (p1 > p2 || q1 > q2)return;
	int p = p1;
	while (mid[p] != front[q1])p++;
	ans[pos] = mid[p];
	solve(p + 1, p2, q1 + (p - p1) + 1, q2, pos << 1);
	solve(p1, p - 1, q1 + 1, q1 + (p - p1), pos << 1 | 1);
}

int main()
{
	int n, flag = 0;
	cin >> n;
	for (int i = 0; i < n; i++)cin >> mid[i];
	for (int i = 0; i < n; i++)cin >> front[i];
	solve(0, n - 1, 0, n - 1, 1);
	for (int i = 1; i < 10000; i++)
	{
		if (ans[i])
		{
			if (flag)cout << " ";
			cout << ans[i];
			flag = 1;
		}
	}
	return 0;
}